<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
  <script>
    let obj = [
      {
        "id": 1,
        "name": "公司地区管理",
        "code": "1",
        "parentCode": "",
        "status": 1,
        "sort": 2,
        "remark": "",
        "children": [
          {
            "id": 2,
            "name": "华南区",
            "code": "10",
            "parentCode": "1",
            "status": 1,
            "sort": 1,
            "remark": "",
            "children": [
              {
                "id": 3,
                "name": "成都市",
                "code": "1010",
                "parentCode": "10",
                "status": 1,
                "sort": 0,
                "remark": ""
              }
            ]
          },
          {
            "id": 6,
            "name": "东北区",
            "code": "20",
            "parentCode": "1",
            "status": 1,
            "sort": 1,
            "remark": "",
          }
        ]
      },
      {
        "id": 4,
        "name": "状态",
        "code": "2",
        "parentCode": "",
        "status": 1,
        "sort": 4,
        "remark": ""
      },
      {
        "id": 5,
        "name": "字典数据",
        "code": "3",
        "parentCode": "",
        "status": 1,
        "sort": 3,
        "remark": ""
      }
    ]
    let code = '2'
    let sort = 2

    // 找到对应的同级数据并排序
    function getDataAndSort(data, code) {
      for (const item of data) {
        if (item.code === code) {
          item.sort = sort - 0.1
          return data.sort((a, b) => a.sort - b.sort)
        }
        if (item.children && item.children.length) {
          let result = getDataAndSort(item.children, code)
          if (result) return result
        }
      }
    }

    let dataList = getDataAndSort(obj, code)
    // 根据新sort再重新计算排序
    if (dataList.length) dataList[0].sort = Math.round(dataList[0].sort)
    for (let i = 1, len = dataList.length; i < len; i++) {
      dataList[i].sort = Math.round(dataList[i].sort)
      if (dataList[i - 1].sort >= dataList[i].sort) {
        dataList[i].sort = dataList[i - 1].sort + 1;
      }
    }
    console.log(dataList)
  </script>
</body>

</html>